Skip to content

fix(plugin-postgresql): keep the selected schema applied after reconnect#1542

Merged
datlechin merged 1 commit into
mainfrom
fix/issue-1540-schema-search-path
Jun 1, 2026
Merged

fix(plugin-postgresql): keep the selected schema applied after reconnect#1542
datlechin merged 1 commit into
mainfrom
fix/issue-1540-schema-search-path

Conversation

@datlechin
Copy link
Copy Markdown
Member

Closes #1540.

Problem

When a schema is selected in TablePro (sidebar picker or the database tree), unqualified table names in the editor stop resolving against it, so the user has to prefix every table (myschema.users).

Root cause

Selecting a schema already runs SET search_path TO "schema", public on the main connection, and editor queries run on that same connection, so it works at first. But LibPQDriverCore silently reconnects on connection loss (executeWithReconnect -> reconnect() -> connect()). connect() re-probes current_schema() (the server default) and never re-applies the user's selection. PostgreSQL drops idle connections routinely, so the first query after a pause triggers a silent reconnect, the search_path reverts, and from then on unqualified queries resolve to the wrong schema. The reconnect is invisible to the layers that would otherwise re-apply the schema, so their recovery never fires.

Fix

The driver core now remembers the user-selected schema and re-applies it whenever a physical connection is established, so search_path stays in sync with the selection across every reconnect path (silent core reconnect, health-monitor, future).

  • LibPQDriverCore stores selectedSchema (distinct from the server-reported currentSchema). connect() re-applies it after the current_schema() probe, updating currentSchema only if the apply succeeds. A new applySchema(_:) records the selection only after the SET succeeds, so a failed switch can't poison later reconnects.
  • switchSchema routes through applySchema, removing the duplicated inline escaping.
  • The SET search_path statement is built by a pure PostgreSQLSchemaQueries.setSearchPath(toSchema:) with identifier quoting (doubles "), so a schema name with quotes can't break out of the identifier.

This covers PostgreSQL, Redshift, and CockroachDB (shared LibPQDriverCore). No PluginKit protocol change, so no ABI bump. The metadata connection pool already scopes its own connections per schema, so introspection is unaffected.

Tests

PostgreSQLSearchPathTests covers the statement builder: plain schema, mixed case, an embedded double quote, and a quote-break injection attempt. It runs in the test target via the existing PluginTestSources symlink (no libpq dependency). The reconnect-restore behavior needs a live PostgreSQL connection, so it is verified at runtime rather than in unit tests.

Notes

  • Oracle has the same gap in its own plugin (ALTER SESSION SET CURRENT_SCHEMA); left as a follow-up since it is a separate registry plugin with its own release.
  • statement_timeout (applyQueryTimeout) has the same reset-on-silent-reconnect pattern. Out of scope here, worth a separate fix.

@datlechin datlechin merged commit e2b79b9 into main Jun 1, 2026
3 of 4 checks passed
@datlechin datlechin deleted the fix/issue-1540-schema-search-path branch June 1, 2026 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possibility to obtain schema in query editor

1 participant